package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.event.ShopCartEvent;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.entity.shop.ShoppingCartJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * 购物车api
 * Created by han on 2019/3/6 0006.
 */

public class ShoppingCartApiIO extends BaseApiIO {
    volatile static ShoppingCartApiIO instance;

    static public ShoppingCartApiIO getInstance() {
        if (instance == null) {
            synchronized (ShoppingCartApiIO.class) {
                if (instance == null) {
                    instance = new ShoppingCartApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 获取购物车列表
     *
     * @param callback
     */
    public void getShopCartList(final APIRequestCallback<ShoppingCartJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_SHOPPING_CART_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取购物车列表===onSuccess");

                ShoppingCartJson mDataJson = JSON.parseObject(res, ShoppingCartJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 加入购物车
     *
     * @param goodsId  商品id
     * @param callback
     */
    public void addToShopCart(String goodsId, final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        addToShopCart(goodsId, null, 1, callback);
    }

    /**
     * 加入购物车
     *
     * @param goodsId  商品id
     * @param skuId    sku id 多个逗号分隔(1,2)
     * @param num      商品数量
     * @param callback
     */
    public void addToShopCart(String goodsId, String skuId, int num, final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("goods_id", goodsId);
        paramsMap.put("goods_sku_id", skuId);
        paramsMap.put("num", String.valueOf(num));

        MainApiIO.getInstance().postRequest(HttpUrls.API_ADD_GOODS_TO_SHOPPING_CART, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("加入购物车===onSuccess");
                //更新购物车
                EventBus.getDefault().post(new ShopCartEvent());

                BaseJson mDataJson = JSON.parseObject(res, BaseJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 删除购物车商品
     *
     * @param ids      商品id 多个逗号分隔(1,2)
     * @param callback
     */
    public void deleteGoodsFromShopCart(String ids, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("ids", ids);

        MainApiIO.getInstance().postRequest(HttpUrls.API_DELETE_GOODS_FROM_SHOPPING_CART, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("删除购物车商品===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 修改购物车商品数量
     *
     * @param shopCartId 购物车id 多个逗号分隔(1,2)
     * @param num        数量
     * @param callback
     */
    public void modifyGoodsNumFromShopCart(String shopCartId, String num, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("shopping_cart_id", shopCartId);
        paramsMap.put("num", num);

        MainApiIO.getInstance().postRequest(HttpUrls.API_SHOPPING_CART_MODIFY_NUM, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("修改购物车商品数量===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取购物车推荐列表
     *
     * @param callback
     */
    public void getShopCartRecommendGoodsList(final APIRequestCallback<ShoppingCartJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_SHOPPING_CART_RECOMMEND_GOODS, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取购物车推荐列表===onSuccess");

                ShoppingCartJson mDataJson = JSON.parseObject(res, ShoppingCartJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    //---------------------智能推荐商品全部加入购物车

    /**
     * 智能推荐商品全部加入购物车
     *
     * @param goodsIds  商品ID字符串 以,分
     * @param callback
     */
    public void addRecommendGoodsToShopCart(String goodsIds, final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("goods_ids", goodsIds);

        MainApiIO.getInstance().postRequest(HttpUrls.API_ADD_RECOMMEND_GOODS_TO_CART, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("智能推荐商品全部加入购物车===onSuccess");

                BaseJson mDataJson = JSON.parseObject(res, BaseJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
